import 'package:aio/aio.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/works_2023/report_loss_bean.dart';

import '../../../manager/user_manager.dart';
import '../user.dart';
import '../wroks/contract_bean.dart';
import '../wroks/payfree_return_bean.dart';

///报损审核详情
class FLoseApplyInfoBean extends ContractCopyInfo {
  //备注
  String? Beizhu = "";

  //入库仓库
  List<StorehouseBean>? StorehouseList = null;

  //当前选中的入库仓库，默认选中第一个。fixme 自己加的字段。用于记录选中仓库
  StorehouseBean? Storehouse = null;

  //获取默认的入库仓库
  StorehouseBean? getDefaultStorehouse() {
    if (Storehouse == null) {
      StorehouseList?.forEach((element) {
        if (element.StorehouseId == AppDataConfig.getFactory()?.FactoryId) {
          Storehouse = element;
        }
      });
      if (Storehouse == null &&
          StorehouseList != null &&
          StorehouseList!.length > 0) {
        Storehouse = StorehouseList![0];
      }
    }
    return Storehouse;
  }

  //检验员
  List<UserInfo>? LoseApplyUserInfo = null;

  //当前选中的检验员。fixme 自己加的字段，用于记录选中检验员。
  UserInfo? Checker = null;

  //获取默认的检验员
  UserInfo? getDefaultChecker() {
    if (Checker == null) {
      LoseApplyUserInfo?.forEach((element) {
        // AppDataConfig.getFactory()
        if (element.PersonId == UserManager.getUserInfo()?.PersonId) {
          Checker = element;
        }
      });
      if (Checker == null &&
          LoseApplyUserInfo != null &&
          LoseApplyUserInfo!.length > 0) {
        Checker = LoseApplyUserInfo![0];
      }
    }
    return Checker;
  }

  //操作班组
  List<GroupInfoBean>? GroupList = null;

  //判断是否为报损补片;报损补片，才有入库仓库，和检验员
  bool isReturnsPatch() {
    if ("报损补片" == YwxName?.trim() ||
        "N6081" == CurrentWfId?.trim() ||
        "N6081" == YwxId?.trim()) {
      return true;
    }
    return false;
  }

  ///报损申请
  bool isN6012() {
    if ("报损申请" == YwxName?.trim() ||
        "N6012" == CurrentWfId?.trim() ||
        "N6012" == YwxId?.trim()) {
      return true;
    }
    return false;
  }

  //详情列表
  List<LoseApplyDetailBean>? LoseApplyDetail = null;

  //报损补片
  List<LoseApplyDetailN6081Bean>? LoseApplyDetailN6081 = null;

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    super.fromJson(json); //fixme 先实现父类的
    Beizhu = json['Beizhu']?.toString() ?? "";
    StorehouseList = setGenericFromJson(
        json, "StorehouseList", <StorehouseBean>[], StorehouseBean());
    LoseApplyUserInfo =
        setGenericFromJson(json, "LoseApplyUserInfo", <UserInfo>[], UserInfo());
    LoseApplyDetail = setGenericFromJson(json, "LoseApplyDetail",
        <LoseApplyDetailBean>[], LoseApplyDetailBean());
    GroupList = setGenericFromJson(
        json, "GroupList", <GroupInfoBean>[], GroupInfoBean());
    LoseApplyDetailN6081 = setGenericFromJson(json, "LoseApplyDetailN6081",
        <LoseApplyDetailN6081Bean>[], LoseApplyDetailN6081Bean());
  }

  @override
  instance() {
    // TODO: implement instance
    return FLoseApplyInfoBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = super.toJson(); //fixme 先实现父类的
    //var map = <String, dynamic>{};
    map["Beizhu"] = Beizhu;
    map["StorehouseList"] = StorehouseList;
    map["LoseApplyUserInfo"] = LoseApplyUserInfo;
    map["LoseApplyDetail"] = LoseApplyDetail;
    //print("=====================================================================入库仓库：$Storehouse");
    map["Storehouse"] = Storehouse;
    map["Checker"] = Checker;
    // print("=====================================================================$Checker");
    // print("=====================================================================$Storehouse");
    map["GroupList"] = GroupList;
    map["LoseApplyDetailN6081"] = LoseApplyDetailN6081;
    return map;
  }
}

///图片
class LoseApplyPics extends BaseBean {
  String? Id = "";
  String? Appcode = "";
  String? Pic = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    Id = json['Id']?.toString() ?? "";
    Appcode = json['Appcode']?.toString() ?? "";
    Pic = json['Pic']?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return LoseApplyPics();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["Id"] = Id;
    map["Appcode"] = Appcode;
    map["Pic"] = Pic;
    return map;
  }
}

///详情列表
class LoseApplyDetailBean extends BaseBean {
  ///fixme 自己添加的字段。
  bool isOpenAn = false; //是否张开，条目详情

  String? OrderCode = ""; //订单编号（任务单号）
  String? ProductName = ""; //产品名称
  String? StockInCode = ""; //入库单号
  String? Num = "";

  int getNum() {
    if (StringUtils.isNotEmpty(Num)) {
      try {
        return double.parse(Num!).toInt();
      } catch (e) {}
    }
    return 0;
  }

  String? Sumarea = "";
  double getSumarea() {
    if (StringUtils.isNotEmpty(Sumarea)) {
      try {
        return double.parse(Sumarea!);
      } catch (e) {}
    }
    return 0;
  }
  String? Amount = "";
  double getAmount() {
    if (StringUtils.isNotEmpty(Amount)) {
      try {
        return double.parse(Amount!);
      } catch (e) {}
    }
    return 0;
  }
  //报损审核
  List<LoseApplyDetailInfoBean>? LoseApplyDetailInfo = null;

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    OrderCode = json['OrderCode']?.toString() ?? "";
    ProductName = json['ProductName']?.toString() ?? "";
    StockInCode = json['StockInCode']?.toString() ?? "";
    Num = json['Num']?.toString() ?? "";
    Sumarea = json['Sumarea']?.toString() ?? "";
    Amount = json['Amount']?.toString() ?? "";
    LoseApplyDetailInfo = setGenericFromJson(json, "LoseApplyDetailInfo",
        <LoseApplyDetailInfoBean>[], LoseApplyDetailInfoBean());
  }

  @override
  instance() {
    // TODO: implement instance
    return LoseApplyDetailBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["OrderCode"] = OrderCode;
    map["ProductName"] = ProductName;
    map["StockInCode"] = StockInCode;
    map["Num"] = Num;
    map["Sumarea"] = Sumarea;
    map["Amount"] = Amount;
    map["LoseApplyDetailInfo"] = LoseApplyDetailInfo;
    return map;
  }
}

///基类
class LoseApplyDetailInfoBean extends BaseBean {
  String? Id = "";
  String? Appcode = "";
  String? ItemCode = ""; //安装编号
  String? ProductName = ""; //产品名称
  String? ItemWidth = ""; //宽
  String? ItemHeight = ""; //高
  //规格型号
  String? getSize() {
    return "${ItemWidth}x${ItemHeight}";
  }

  String? Num = ""; //退货数量;(或报损数量)
  //获取退货数量
  int getTHNum() {
    try {
      var str = Num?.trim();
      if (StringUtils.isNotEmpty(str)) {
        var d = double.parse(str!);
        return d.toInt();
      }
    } catch (e) {}
    return 0;
  }

  String? Area = "";
  String? Price = ""; //单价
  String? Amount = ""; //金额
  String? PositionCode = "";
  String? ErpId = "";
  String? OrderCode = "";
  String? ShelfBodyId = "";
  String? StockInCode = ""; //入库单号
  String? ItemId = "";
  String? TrueSqm = ""; //实际面积
  String? ItemWeight = "";
  String? PriceUnit = "";
  String? OtherAmount = "";
  String? SumArea = ""; //面积
  String? SumSqm = "";
  String? SumWeight = "";
  String? FactoryId = "";
  String? FactoryName = "";
  String? HandleMode = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    Id = json['Id']?.toString() ?? "";
    Appcode = json['Appcode']?.toString() ?? "";
    ItemCode = json['ItemCode']?.toString() ?? "";
    ProductName = json['ProductName']?.toString() ?? "";
    ItemWidth = json['ItemWidth']?.toString() ?? "";
    ItemHeight = json['ItemHeight']?.toString() ?? "";
    Num = json['Num']?.toString() ?? "";
    Area = json['Area']?.toString() ?? "";
    Price = json['Price']?.toString() ?? "";
    Amount = json['Amount']?.toString() ?? "";
    PositionCode = json['PositionCode']?.toString() ?? "";
    ErpId = json['ErpId']?.toString() ?? "";
    OrderCode = json['OrderCode']?.toString() ?? "";
    ShelfBodyId = json['ShelfBodyId']?.toString() ?? "";
    StockInCode = json['StockInCode']?.toString() ?? "";
    ItemId = json['ItemId']?.toString() ?? "";
    TrueSqm = json['TrueSqm']?.toString() ?? "";
    ItemWeight = json['ItemWeight']?.toString() ?? "";
    PriceUnit = json['PriceUnit']?.toString() ?? "";
    OtherAmount = json['OtherAmount']?.toString() ?? "";
    SumArea = json['SumArea']?.toString() ?? "";
    SumSqm = json['SumSqm']?.toString() ?? "";
    SumWeight = json['SumWeight']?.toString() ?? "";
    FactoryId = json['FactoryId']?.toString() ?? "";
    FactoryName = json['FactoryName']?.toString() ?? "";
    HandleMode = json['HandleMode']?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return LoseApplyDetailInfoBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["Id"] = Id;
    map["Appcode"] = Appcode;
    map["ItemCode"] = ItemCode;
    map["ProductName"] = ProductName;
    map["ItemWidth"] = ItemWidth;
    map["ItemHeight"] = ItemHeight;
    map["Num"] = Num;
    map["Area"] = Area;
    map["Price"] = Price;
    map["Amount"] = Amount;
    map["PositionCode"] = PositionCode;
    map["ErpId"] = ErpId;
    map["OrderCode"] = OrderCode;
    map["ShelfBodyId"] = ShelfBodyId;
    map["StockInCode"] = StockInCode;
    map["ItemId"] = ItemId;
    map["TrueSqm"] = TrueSqm;
    map["ItemWeight"] = ItemWeight;
    map["PriceUnit"] = PriceUnit;
    map["OtherAmount"] = OtherAmount;
    map["SumArea"] = SumArea;
    map["SumSqm"] = SumSqm;
    map["SumWeight"] = SumWeight;
    map["FactoryId"] = FactoryId;
    map["FactoryName"] = FactoryName;
    map["HandleMode"] = HandleMode;
    return map;
  }
}

///报损补片N6081时需要的数据
class LoseApplyDetailN6081Bean extends LoseApplyDetailInfoBean {
  String? IsUse = "";
  String? WasteNum = ""; //报废数量，初始值和退回数量（num）相同
  //获取报废数量
  int getWasteNum() {
    try {
      var str = WasteNum?.trim();
      if (StringUtils.isNotEmpty(str)) {
        var d = double.parse(str!);
        return d.toInt();
      }
    } catch (e) {}
    return 0;
  }

  String? CPNum = ""; //拆片数量，初始值为0
  //获取拆片数量
  int getCPNum() {
    try {
      var str = CPNum?.trim();
      if (StringUtils.isNotEmpty(str)) {
        var d = double.parse(str!);
        return d.toInt();
      }
    } catch (e) {}
    return 0;
  }

  String? BackNum = ""; //退库数量（自动）：num-wastenum-CPnum
  //获取退库数量
  String getBackNumStr() {
    try {
      var num =
          double.parse(Num!) - double.parse(WasteNum!) - double.parse(CPNum!);
      var str = num.toInt().toString();
      BackNum = str;
      return str;
    } catch (e) {}
    return "0";
  }

  //下列：报损补片N6081时需要的数据，减少重复代码
  String? GlassName = "";
  String? OrderItemId = "";
  String? BodyIdList = "";
  String? LastHpProcessId = "";

  ///是否张开，条目详情
  bool isOpenAn() {
    if (StringUtils.isNotEmpty(mList) && getCPNum() > 0) {
      return true;
    }
    return false;
  }

  //退货补片子数据
  List<PayFreeGoodReturnChild>? mList = null;

  //初始化拆片数据,防止拆片数量，大于可拆片数据。
  void initList() {
    var cp = getCPNum(); //拆片数据
    mList?.forEach((element) {
      if (element.getCPNum() > cp) {
        element.CPNum = cp.toString();
      }
    });
  }

  //自己定义的，当前选中的操作班组
  String? GroupId = "";
  String? GroupName = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    super.fromJson(json); //fixme 先实现父类的
    IsUse = json['IsUse']?.toString() ?? "";
    WasteNum = json['WasteNum']?.toString() ?? "0";
    CPNum = json['CPNum']?.toString() ?? "0";
    BackNum = json['BackNum']?.toString() ?? "0";
    GlassName = json['GlassName']?.toString() ?? "";
    OrderItemId = json['OrderItemId']?.toString() ?? "";
    BodyIdList = json['BodyIdList']?.toString() ?? "";
    LastHpProcessId = json['LastHpProcessId']?.toString() ?? "";
    mList = setGenericFromJson(
        json, "mList", <PayFreeGoodReturnChild>[], PayFreeGoodReturnChild());
    GroupId = json['GroupId']?.toString() ?? "";
    GroupName = json['GroupName']?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return LoseApplyDetailN6081Bean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = super.toJson(); //fixme 先实现父类的
    //var map = <String, dynamic>{};
    map["IsUse"] = IsUse;
    map["WasteNum"] = WasteNum;
    map["CPNum"] = CPNum;
    map["BackNum"] = BackNum;
    map["GlassName"] = GlassName;
    map["OrderItemId"] = OrderItemId;
    map["BodyIdList"] = BodyIdList;
    map["LastHpProcessId"] = LastHpProcessId;
    map["mList"] = mList;
    map["GroupId"] = GroupId;
    map["GroupName"] = GroupName;
    return map;
  }
}
